<p id="notice"><%= notice %></p>

<p>
  <b>Chat name:</b>
  <span id="chat_name"><%= @chat.name %></span>
</p>

<ul id="users">
  <h3>Present users</h3>
  <% present_users.each do |user| %>
    <%= user.username %>
  <% end %>
</ul>

<div id="messages">
  <h3>Messages</h3>
  <% @chat.messages.each do |msg| %>
    <div class="message">
      <%= simple_format msg.text %>
    </div>
    <hr/>
  <% end %>
</div>

<%= form_for [@chat, @chat.messages.build] do |f| %>
  <h3>New Message</h3>
  <%= f.text_area :text, :rows => 5 %>

  <p>
    <%= f.submit 'send' %>
  </p>
<% end %>

<%= link_to 'Back', chats_path %>

<%= javascript_include_tag 'alondra-client' %>

<script type="text/javascript">
  var client = new AlondraClient('localhost:<%= Alondra::Alondra.config.port %>', '<%= chat_path(@chat) %>', '<%= encrypted_token %>');

  $(client).bind('subscribed.Chat', function(event, resource){
    console.log('Received subscribed event!');
    $('#messages').append('<div class="message">Subscribed to channel</div>');
  })

  $(client).bind('updated.Chat', function(event, resource){
    $('#chat_name').text(resource.name);
  })
</script>
